<?php

namespace App\Module;

use App\depository\RecordFlyDep;
use App\depository\SteamIDEosidDep;
use App\depository\RecordKillDep;
use App\depository\RecordTeamDep;
use App\depository\RecordBunkoDep;
use App\depository\RecordPlayerDep;
use App\depository\RecordTalkDep;
use App\depository\RecordFriendKillDep;
use App\depository\TeamsDep;
use App\depository\TeamDictDep;

class RecordModule extends BaseModule
{
    public function fly_add($request)
    {
        $params = $request->all();
//        log_daily(__FUNCTION__)->info(json_encode($params, JSON_UNESCAPED_UNICODE));
        $steam_id = $params['SteamID'];
        $eos_id = $params['EosID'];
        $date = $params['date'];
        $player_name = $params['player_name'];
        $player_handle = $params['player_handle'];
        $gh_id = $params['gh_id'];
        $gh_secret = $params['gh_secret'];
        $res = (new TeamsDep)->check_exists($gh_id, $gh_secret);
        if (is_null($res)){
            return self::response([], "工会ID与密钥不匹配", 2);
        }
        $SteamIDEosidDep = new SteamIDEosidDep;
        if(! $steam_id && $eos_id){
            $steam_id = $SteamIDEosidDep->get_fields(['steamid'], 'eosid', $eos_id)->steamid ?? "";
        }
        if(! $eos_id && $steam_id){
            $eos_id = $SteamIDEosidDep->get_fields(['eosid'], 'steamid', $steam_id)->eosid ?? "";
        }

        try{
            $data = [
                'steamid' => $steam_id,
                'eosid' => $eos_id,
                'date' => $date,
                'player_name' => $player_name,
                'player_handle' => $player_handle,
                'gh_id' => $gh_id
            ];
            (new RecordFlyDep)->add($data);
        } catch(\Exception  $e){
            log_daily(__FUNCTION__)->info($e->getMessage());
        }
        return self::response();
    }

    public function killrecord_add($request)
    {
        $params = $request->all();
//        log_daily(__FUNCTION__)->info(json_encode($params, JSON_UNESCAPED_UNICODE));
        $date = $params['date'];
        $murderer = $params['murderer'];
        $murderer_SteamID = $params['murderer_SteamID'];
        $hurt_type = $params['hurt_type'];
        $hurt_num = $params['hurt_num'];
        $victim = $params['victim'];
        $victim_SteamID = $params['victim_SteamID'];
        $weapon = $params['weapon'];
        $gh_id = $params['gh_id'];
        $murderer_eosid = $params['murderer_EosID'];
        $victim_eosid = $params['victim_EosID'];
        $gh_secret = $params['gh_secret'];
        $res = (new TeamsDep)->check_exists($gh_id, $gh_secret);
        if (is_null($res)){
            return self::response([], "工会ID与密钥不匹配", 2);
        }
        $SteamIDEosidDep = new SteamIDEosidDep;
        if($murderer_SteamID && ! $murderer_eosid){
            $murderer_eosid = $SteamIDEosidDep->get_fields(['eosid'], 'steamid', $murderer_SteamID)->eosid ?? "";
        }
        if(! $murderer_SteamID && $murderer_eosid){
            $murderer_SteamID = $SteamIDEosidDep->get_fields(['steamid'], 'eosid', $murderer_eosid)->steamid ?? "";
        }

        if(! $victim_SteamID && $victim_eosid){
            $victim_SteamID = $SteamIDEosidDep->get_fields(['steamid'], 'eosid', $victim_eosid)->steamid ?? "";
        }
        if($victim_SteamID && ! $victim_eosid){
            $victim_eosid = $SteamIDEosidDep->get_fields(['eosid'], 'steamid', $victim_SteamID)->eosid ?? "";
        }
        try{
            $data = [
                'date' => $date,
                'murderer' => $murderer,
                'murderer_steamid' => $murderer_SteamID,
                'murderer_eosid' => $murderer_eosid,
                'hurt_type' => $hurt_type,
                'hurt_num' => $hurt_num,
                'victim' => $victim,
                'victim_steamid' => $victim_SteamID,
                'victim_eosid' => $victim_eosid,
                'weapon' => $weapon,
                'gh_id' => $gh_id
            ];
            (new RecordKillDep)->add($data);
        } catch(\Exception $e){
            log_daily(__FUNCTION__)->info($e->getMessage());
        }
        return self::response();
    }

    public function teamrecord_add($request)
    {
        $params = $request->all();
//        log_daily(__FUNCTION__)->info(json_encode($params, JSON_UNESCAPED_UNICODE));
        $date = $params['date'];
        $player_name = $params['player_name'];
        $SteamID = $params['SteamID'];
        $team_id = $params['team_id'];
        $team_name = $params['team_name'];
        $camp = $params['camp'];
        $gh_id = $params['gh_id'];
        $eosid = $params['EosID'];
        $gh_secret = $params['gh_secret'];
        $res = (new TeamsDep)->check_exists($gh_id, $gh_secret);
        if (is_null($res)){
            return self::response([], "工会ID与密钥不匹配", 2);
        }
        $SteamIDEosidDep = new SteamIDEosidDep;
        if(! $SteamID && $eosid){
            $SteamID = $SteamIDEosidDep->get_fields(['steamid'], 'eosid', $eosid)->steamid ?? "";
        }
        if(! $eosid && $SteamID){
            $eosid = $SteamIDEosidDep->get_fields(['eosid'], 'steamid', $SteamID)->eosid ?? "";
        }
        try{
            $data = [
                'steamid' => $SteamID,
                'eosid' => $eosid,
                'date' => $date,
                'player_name' => $player_name,
                'team_name' => $team_name,
                'team_id' => $team_id,
                'camp' => $camp,
                'gh_id' => $gh_id
            ];
            (new RecordTeamDep)->add($data);
        } catch(\Exception $e){
            log_daily(__FUNCTION__)->info($e->getMessage());
        }
        return self::response();
    }

    public function bunkorecord_add($request)
    {
        $params = $request->all();
//        log_daily(__FUNCTION__)->info(json_encode($params, JSON_UNESCAPED_UNICODE));
        $date = $params['date'];
        $camp_id = $params['camp_id'];
        $camp_name = $params['camp_name'];
        $bunko_result = $params['bunko_result'];
        $ticket_num = $params['ticket_num'];
        $map_name = $params['map_name'];
        $gh_id = $params['gh_id'];
        $gh_secret = $params['gh_secret'];
        $res = (new TeamsDep)->check_exists($gh_id, $gh_secret);
        if (is_null($res)){
            return self::response([], "工会ID与密钥不匹配", 2);
        }
        try{
            $data = [
                'date' => $date,
                'player_name' => "",
                'camp_id' => $camp_id,
                'camp_name' => $camp_name,
                'bunko_result' => $bunko_result,
                'ticket_num' => $ticket_num,
                'map_name' => $map_name,
                'gh_id' => $gh_id
            ];
            (new RecordBunkoDep)->add($data);
        } catch(\Exception $e){
            log_daily(__FUNCTION__)->info($e->getMessage());
        }
        return self::response();
    }

    public function talkrecord_add($request)
    {
        $params = $request->all();
//        log_daily(__FUNCTION__)->info(json_encode($params, JSON_UNESCAPED_UNICODE));
        $date = $params['date'];
        $player_name = $params['player_name'];
        $SteamID = $params['SteamID'];
        $eosid = $params["EosID"];
        $channel = $params['channel'];
        $content = $params['content'];
        $gh_id = $params['gh_id'];
        $gh_secret = $params['gh_secret'];
        $res = (new TeamsDep)->check_exists($gh_id, $gh_secret);
        if (is_null($res)){
            return self::response([], "工会ID与密钥不匹配", 2);
        }
        $SteamIDEosidDep = new SteamIDEosidDep;
        if(! $SteamID && $eosid){
            $SteamID = $SteamIDEosidDep->get_fields(['steamid'], 'eosid', $eosid)->steamid ?? "";
        }
        if(! $eosid && $SteamID){
            $eosid = $SteamIDEosidDep->get_fields(['eosid'], 'steamid', $SteamID)->eosid ?? "";
        }
        try{
            $data = [
                'steamid' => $SteamID,
                'eosid' => $eosid,
                'date' => $date,
                'player_name' => $player_name,
                'channel' => $channel,
                'content' => $content,
                'gh_id' => $gh_id
            ];
            (new RecordTalkDep)->add($data);
        } catch(\Exception $e){
            log_daily(__FUNCTION__)->info($e->getMessage());
        }
        return self::response();
    }

    public function playrecord_add($request)
    {
        $params = $request->all();
//        log_daily(__FUNCTION__)->info(json_encode($params, JSON_UNESCAPED_UNICODE));
        $date = $params['date'];
        $player_name = $params['player_name'];
        $SteamID = $params['SteamID'];
        $content = $params['content'];
        $gh_id = $params['gh_id'];
        $eosid = $params['EosID'];
        $gh_secret = $params['gh_secret'];
        $res = (new TeamsDep)->check_exists($gh_id, $gh_secret);
        if (is_null($res)){
            return self::response([], "工会ID与密钥不匹配", 2);
        }
        $SteamIDEosidDep = new SteamIDEosidDep;
        if(! $SteamID && $eosid){
            $SteamID = $SteamIDEosidDep->get_fields(['steamid'], 'eosid', $eosid)->steamid ?? "";
        }
        if(! $eosid && $SteamID){
            $eosid = $SteamIDEosidDep->get_fields(['eosid'], 'steamid', $SteamID)->eosid ?? "";
        }
        try{
            $data = [
                'steamid' => $SteamID,
                'eosid' => $eosid,
                'date' => $date,
                'player_name' => $player_name,
                'content' => $content,
                'gh_id' => $gh_id
            ];
            (new RecordPlayerDep)->add($data);
        } catch(\Exception $e){
            log_daily(__FUNCTION__)->info($e->getMessage());
        }
        return self::response();
    }

    public function friendkillrecord_add($request)
    {
        $params = $request->all();
//        log_daily(__FUNCTION__)->info(json_encode($params, JSON_UNESCAPED_UNICODE));
        $date = $params['date'];
        $murderer_name = $params['murderer_name'];
        $murderer_steamid = $params['murderer_steamid'];
        $murderer_eosid = $params['murderer_eosid'];
        $victim_name = $params['victim_name'];
        $victim_steamid = $params['victim_steamid'];
        $victim_eosid = $params['victim_eosid'];
        $gh_id = $params['gh_id'];
        $gh_secret = $params['gh_secret'];
        $res = (new TeamsDep)->check_exists($gh_id, $gh_secret);
        if (is_null($res)){
            return self::response([], "工会ID与密钥不匹配", 2);
        }

        $SteamIDEosidDep = new SteamIDEosidDep;
        if(! $murderer_steamid && $murderer_eosid){
            $murderer_steamid = $SteamIDEosidDep->get_fields(['steamid'], 'eosid', $murderer_eosid)->steamid ?? "";
        }
        if(! $murderer_eosid && $murderer_steamid){
            $murderer_eosid = $SteamIDEosidDep->get_fields(['eosid'], 'steamid', $murderer_steamid)->eosid ?? "";
        }

        if(! $victim_steamid && $victim_eosid){
            $victim_steamid = $SteamIDEosidDep->get_fields(['steamid'], 'eosid', $victim_eosid)->steamid ?? "";
        }
        if(! $victim_eosid && $victim_steamid){
            $victim_eosid = $SteamIDEosidDep->get_fields(['eosid'], 'steamid', $victim_steamid)->eosid ?? "";
        }

        try{
            $data = [
                'date' => $date,
                'murderer_name' => $murderer_name,
                'murderer_steamid' => $murderer_steamid,
                'murderer_eosid' => $murderer_eosid,
                'victim_name' => $victim_name,
                'victim_steamid' => $victim_steamid,
                'victim_eosid' => $victim_eosid,
                'gh_id' => $gh_id
            ];
            (new RecordFriendKillDep)->add($data);
        } catch(\Exception $e){
            log_daily(__FUNCTION__)->info($e->getMessage());
        }
        return self::response();
    }

    public function record_get($request)
    {
        $params = $request->all();
        $gh_id = $params['gh_id'];
        $gh_secret = $params['gh_secret'];
        $type = $params['type'];
        $res = (new TeamsDep)->check_exists($gh_id, $gh_secret);
        if (is_null($res)){
            return self::response([], "工会ID与密钥不匹配", 2);
        }
        $module = [
            1 => RecordFlyDep::class,
            2 => RecordKillDep::class,
            3 => RecordTeamDep::class,
            4 => RecordBunkoDep::class,
            5 => RecordTalkDep::class,
            6 => RecordPlayerDep::class,
            7 => RecordFriendKillDep::class,
            8 => TeamDictDep::class
        ];
        $record = new $module[$type];
        $list = $record->get_list_by_gh_id($gh_id);
        return self::response($list);
    }
}

